//
//  UINavigationItem+FastItem.m
//  JFBase
//
//  Created by Alan Wang on 2018/11/30.
//  Copyright © 2018 Alan Wang. All rights reserved.
//

#import "UINavigationItem+FastItem.h"
#import "JFImageHelper.h"
#import "JFBase.h"

#define AWSysNavbarHeight 44
//标题字体大小
#define AWTitleFont 20
//标题字体颜色
#define AWTitleColor [UIColor whiteColor]
//普通按钮的宽度
#define AWItemWidth 35
//普通按钮的高度
#define AWItemHeight 40
//普通按钮的字体大小
#define AWItemTextFont 16
//普通按钮的文字颜色
#define AWItemTextNormalColot [UIColor whiteColor]
//普通按钮的文字选中颜色
#define AWItemTextSelectedColot [UIColor colorWithWhite:0.7 alpha:1]

@implementation UINavigationItem (FastItem)

- (void)setAWTitle:(NSString *)title{
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, 180, 20);
    label.backgroundColor = [UIColor clearColor];
    label.tag = 8888;
    label.font = [UIFont boldSystemFontOfSize:AWTitleFont];
    label.textColor = AWTitleColor;
    [label sizeToFit];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = title;
    self.titleView = label;
}

- (void)setImageTitle:(UIImage *)image
{
    if(image){
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.tag = 8887;
        self.titleView = imageView;
    }
}


#pragma mart-- 左侧快捷方法
- (void)leftItemWithTarget:(id)target action:(SEL)action title:(NSString *)title{
    UIButton * btn = [self creatBtn];
    [self setTitle:btn title:title];
    [self setTarget:btn target:target withAction:action];
    self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
- (void)leftItemWithTarget:(id)target action:(SEL)action image:(UIImage *)image{
    UIButton * btn = [self creatBtn];
    [self setImage:btn image:image];
    [self setTarget:btn target:target withAction:action];
    self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
//自定义的视图
-(void)leftItemWithCustomView:(UIView*)view{
    if(view){
        self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    }
}

- (void)leftItemWithTarget:(id)target action:(SEL)action title:(NSString *)title space:(CGFloat)space{
    UIButton * btn = [self creatBtn];
    [self setTitle:btn title:title];
    [self setTarget:btn target:target withAction:action];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.leftBarButtonItems = [self getItemArr:item space:space];
}

- (void)leftItemWithTarget:(id)target action:(SEL)action image:(UIImage *)image space:(CGFloat)space{
    UIButton * btn = [self creatBtn];
    [self setImage:btn image:image];
    [self setTarget:btn target:target withAction:action];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.leftBarButtonItems = [self getItemArr:item space:space];
}
//自定义的视图
-(void)leftItemWithCustomView:(UIView*)view space:(CGFloat)space{
    if(view){
        UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:view];
        self.leftBarButtonItems = [self getItemArr:item space:space];
    }
}

#pragma mark--右侧快捷方法
//设置右侧文本按钮
- (void)rightItemWithTarget:(id)target action:(SEL)action title:(NSString *)title{
    UIButton * btn = [self creatBtn];
    [self setTitle:btn title:title];
    [self setTarget:btn target:target withAction:action];
    self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
//设置右侧图片按钮
- (void)rightItemWithTarget:(id)target action:(SEL)action image:(UIImage *)image{
    UIButton * btn = [self creatBtn];
    [self setImage:btn image:image];
    [self setTarget:btn target:target withAction:action];
    self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
//自定义的视图
-(void)rightItemWithCustomView:(UIView*)view{
    if(view){
        self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    }
}


//设置右侧文本按钮
- (void)rightItemWithTarget:(id)target action:(SEL)action title:(NSString *)title space:(CGFloat)space{
    UIButton * btn = [self creatBtn];
    [self setTitle:btn title:title];
    [self setTarget:btn target:target withAction:action];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.rightBarButtonItems = [self getItemArr:item space:space];
}
//设置右侧图片按钮
- (void)rightItemWithTarget:(id)target action:(SEL)action image:(UIImage *)image space:(CGFloat)space{
    UIButton * btn = [self creatBtn];
    [self setImage:btn image:image];
    [self setTarget:btn target:target withAction:action];
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.rightBarButtonItems = [self getItemArr:item space:space];
}
//自定义的视图
-(void)rightItemWithCustomView:(UIView*)view space:(CGFloat)space{
    if(view){
        UIBarButtonItem * item =  [[UIBarButtonItem alloc] initWithCustomView:view];
        self.rightBarButtonItems = [self getItemArr:item space:space];
    }
}

-(NSArray*)getItemArr:(UIBarButtonItem*)item space:(CGFloat)space{
    if (@available(iOS 7.0, *))
    {
       
    }else{
         space = space + 10;
    }
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    [negativeSpacer setWidth:space];
    return [NSArray arrayWithObjects:negativeSpacer,item,nil];
}

#pragma mark--辅助方法
-(UIButton*)creatBtn{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font  = [UIFont systemFontOfSize:AWItemTextFont];
    [button setTitleColor:AWThemeColorForKey(@"Global.navTintColor") forState:UIControlStateNormal];
//    [button setTitleColor:AWThemeColorForKey(@"Global.navTintColor") forState:UIControlStateHighlighted];
//    [button setTitleColor:AWThemeColorForKey(@"Global.navTintColor") forState:UIControlStateSelected];
    button.tag = 4444;
    button.frame = CGRectMake(0, 0, AWItemWidth, AWItemHeight);
    return button;
}
- (void)setTarget:(UIButton*)button  target:(id)target withAction:(SEL)action
{
     if(!target||!button||!action)return;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setTitle:(UIButton*)button title:(NSString *)title
{
    if(!title||!button)return;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateSelected];
}

- (void)setImage:(UIButton*)button  image:(UIImage *)image
{
    if(!image||!button)return;
    image = [JFImageHelper modifyImageSize:CGSizeMake(22, 22) OfImage:image];
    UIButton *imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(button.frame.size.width-image.size.width, (button.frame.size.height -image.size.height)/2, image.size.width, image.size.height)];
    imageBtn.userInteractionEnabled = NO;
    [imageBtn setImage:image forState:UIControlStateNormal];
    [imageBtn setImage:image forState:UIControlStateHighlighted];
    [imageBtn setImage:image forState:UIControlStateSelected];
    [button addSubview:imageBtn];
}

- (void)setLeftImage:(UIButton*)button  image:(UIImage *)image
{
    if(!image||!button)return;
    image = [JFImageHelper modifyImageSize:CGSizeMake(22, 22) OfImage:image];
    UIButton *imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, (button.frame.size.height -image.size.height)/2, image.size.width, image.size.height)];
    imageBtn.userInteractionEnabled = NO;
    [imageBtn setImage:image forState:UIControlStateNormal];
    [imageBtn setImage:image forState:UIControlStateHighlighted];
    [imageBtn setImage:image forState:UIControlStateSelected];
    [button addSubview:imageBtn];
}

- (void)setNormalColor:(UIButton*)button normalColor:(UIColor *)normalColor
{
    if(!normalColor||!button)return;
    [button setTitleColor:normalColor forState:UIControlStateNormal];
}

- (void)setSelectedColor:(UIButton*)button selectedColor:(UIColor *)selectedColor
{
    if(!selectedColor||!button)return;
    [button setTitleColor:selectedColor forState:UIControlStateHighlighted];
    [button setTitleColor:selectedColor forState:UIControlStateSelected];
}

@end
